

const sql = require('mssql');
const baseFn = require('../../public/base.js');
/**
 * 
 *报损接口
 */

//  获取报损商品的信息
const getReportLossShop = async (ctx, next) => {

    const { searchList, index, pageSize } = ctx.request.body;
    let selectSql = `SELECT * FROM(SELECT ROW_NUMBER() OVER(ORDER BY BM.ID) AS px,  BZ.RQ,BZ.ID,BM.YPBM,BM.YPMC,BM.PZWH,BM.SCCJ,BM.SCPH,BM.SL,BM.DJ,BM.JE,BM.BSYY,BZ.BZ,BZ.BMMC,BZ.JBRMC,BM.YPGG,BM.SCRQ,
    BM.YXQZ,BZ.ZDR,BZ.SHR,BM.JLDW from BSD_MX BM LEFT JOIN BSD_ZB BZ ON BM.ID = BZ.ID LEFT JOIN BM_YP YP ON BM.YPBM = YP.BM WHERE BZ.SH = 'T'`;
    let totalSql = ` SELECT COUNT(BM.ID) AS TOTAL from BSD_MX BM LEFT JOIN BSD_ZB BZ ON BM.ID = BZ.ID LEFT JOIN BM_YP YP ON BM.YPBM = YP.BM 
    WHERE BZ.SH = 'T' `;
    let searchSqlList = [];
    if (searchList.rangeTimes[0]) {
        searchSqlList.push(` AND BZ.RQ >= '${searchList.rangeTimes[0]} 00:00:00' AND BZ.RQ <= '${searchList.rangeTimes[1]} 23:59:59' `);
    }
    if (searchList.ID) {
        searchSqlList.push(` AND BZ.ID = '${searchList.ID}' `);
    }
    if (searchList.YPBM) {
        searchSqlList.push(` AND (YP.BM LIKE '%${searchList.YPBM}%' OR YP.MC LIKE '%${searchList.YPBM}%' OR YP.JP LIKE '%${searchList.YPBM}%'  ) `);
    }
    totalSql += searchSqlList.join(' ')
    selectSql += ` ${searchSqlList.join(' ')})AS t1 WHERE  t1.px between ${(index - 1) * pageSize + 1} and ${index * 10} `
    let result = await sql.query(selectSql);
    let total = await sql.query(totalSql);
    ctx.body = { total: total.recordset[0].TOTAL, result: result.recordset }

}

// 获取导出的报损商品的信息

const getReportLossExportData = async (ctx, next) => {
    try {
        const { searchList } = ctx.request.body;
        let selectSql = `SELECT   BZ.RQ,BZ.ID,BM.YPBM,BM.YPMC,BM.PZWH,BM.SCCJ,BM.SCPH,BM.SL,BM.DJ,BM.JE,BM.BSYY,BZ.BZ,BZ.BMMC,BZ.JBRMC,BM.YPGG,BM.SCRQ,
    BM.YXQZ,BZ.ZDR,BZ.SHR,BM.JLDW from BSD_MX BM LEFT JOIN BSD_ZB BZ ON BM.ID = BZ.ID LEFT JOIN BM_YP YP ON BM.YPBM = YP.BM WHERE BZ.SH = 'T'`;

        let searchSqlList = [];
        if (searchList.rangeTimes[0]) {
            searchSqlList.push(` AND BZ.RQ >= '${searchList.rangeTimes[0]} 00:00:00' AND BZ.RQ <= '${searchList.rangeTimes[1]} 23:59:59' `);
        }
        if (searchList.ID) {
            searchSqlList.push(` AND BZ.ID = '${searchList.ID}' `);
        }
        if (searchList.YPBM) {
            searchSqlList.push(` AND (YP.BM LIKE '%${searchList.YPBM}%' OR YP.MC LIKE '%${searchList.YPBM}%' OR YP.JP LIKE '%${searchList.YPBM}%'  ) `);
        }

        selectSql += ` ${searchSqlList.join(' ')} `
        let result = await sql.query(selectSql);
        if (result.rowsAffected[0] > 0) {
            ctx.body = { code: 200, message: '获取商品信息成功', data: { result: result.recordset } }
        } else {
            ctx.body = { code: 400, message: '未获取到商品信息' }
        }
    } catch (err) {
        ctx.body = { code: 500, err }
    }
}


module.exports = {
    'POST /getReportLossShop': getReportLossShop,
    'POST /getReportLossExportData': getReportLossExportData
}
